1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2018 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview Read and write to buffers. |
27
|
|
|
* @see https://github.com/rochars/wavefile |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
import {unpackFrom, unpackString, packString} from '../vendor/byte-data.js'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A class to read and write to buffers. |
34
|
|
|
*/ |
35
|
|
|
export default class BufferIO { |
36
|
|
|
|
37
|
|
|
constructor() { |
38
|
|
|
/** |
39
|
|
|
* @type {number} |
40
|
|
|
* @private |
41
|
|
|
*/ |
42
|
|
|
this.head_ = 0; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Write a variable size string as bytes. If the string is smaller |
47
|
|
|
* than the max size the output array is filled with 0s. |
48
|
|
|
* @param {string} str The string to be written as bytes. |
49
|
|
|
* @param {number} maxSize the max size of the string. |
50
|
|
|
* @return {!Array<number>} The bytes. |
51
|
|
|
* @private |
52
|
|
|
*/ |
53
|
|
|
writeString_(str, maxSize, push=true) { |
54
|
|
|
/** @type {!Array<number>} */ |
55
|
|
|
let bytes = packString(str); |
56
|
|
|
if (push) { |
57
|
|
|
for (let i=bytes.length; i<maxSize; i++) { |
58
|
|
|
bytes.push(0); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
return bytes; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Read bytes as a ZSTR string. |
66
|
|
|
* @param {!Uint8Array} bytes The bytes. |
67
|
|
|
* @return {string} The string. |
68
|
|
|
* @private |
69
|
|
|
*/ |
70
|
|
|
readZSTR_(bytes, index=0) { |
71
|
|
|
/** @type {string} */ |
72
|
|
|
let str = ''; |
73
|
|
|
for (let i=index; i<bytes.length; i++) { |
74
|
|
|
this.head_++; |
75
|
|
|
if (bytes[i] === 0) { |
76
|
|
|
break; |
77
|
|
|
} |
78
|
|
|
str += unpackString(bytes, i, 1); |
79
|
|
|
} |
80
|
|
|
return str; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Read bytes as a string from a RIFF chunk. |
85
|
|
|
* @param {!Uint8Array} bytes The bytes. |
86
|
|
|
* @param {number} maxSize the max size of the string. |
87
|
|
|
* @return {string} The string. |
88
|
|
|
* @private |
89
|
|
|
*/ |
90
|
|
|
readString_(bytes, maxSize) { |
91
|
|
|
/** @type {string} */ |
92
|
|
|
let str = ''; |
93
|
|
|
for (let i=0; i<maxSize; i++) { |
94
|
|
|
str += unpackString(bytes, this.head_, 1); |
95
|
|
|
this.head_++; |
96
|
|
|
} |
97
|
|
|
return str; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Read a number from a chunk. |
102
|
|
|
* @param {!Uint8Array} bytes The chunk bytes. |
103
|
|
|
* @param {!Object} bdType The type definition. |
104
|
|
|
* @return {number} The number. |
105
|
|
|
* @private |
106
|
|
|
*/ |
107
|
|
|
read_(bytes, bdType) { |
108
|
|
|
/** @type {number} */ |
109
|
|
|
let size = bdType.bits / 8; |
110
|
|
|
/** @type {number} */ |
111
|
|
|
let value = unpackFrom(bytes, bdType, this.head_); |
112
|
|
|
this.head_ += size; |
113
|
|
|
return value; |
114
|
|
|
} |
115
|
|
|
} |